<template>
  <div class="custom-body" :style="borderStyle">
    <div
      v-if="column.sfxsbt === null ? showHeader : column.sfxsbt"
      class="custom-header"
      :style="titleStyle"
    >
      <div class="header-body" :style="headerStyle">
        <div v-if="temp.sfsxtb === true" class="header-icon">
          <i :class="temp.tb" />
        </div>
        <div v-else class="header-line" />
        <div class="header-title">
          <span class="title-span">{{ temp.name }}</span>
        </div>
        <div
          v-if="temp.isMore === true"
          class="header-icon more"
          :style="titleFont"
          @click="openMore"
        >
          更多
        </div>
      </div>
    </div>
    <el-empty
      v-if="dataList === null || dataList.length === 0"
      class="custom-right"
      :image-size="100"
    />
    <div v-else class="custom-right">
      <div
        v-for="(item, index) in dataList"
        :key="'server-' + index"
        :style="liStyle"
        class="custom-right-li"
        @click="openEntity(item)"
      >
        <div class="custom-li-body">
          <div class="custom-li-image">
            <el-image
              lazy
              class="custom-image"
              :src="baseURL + item[column['image']]"
            />
          </div>
          <div class="custom-li-title">{{ item[column["title"]] }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryAllSystemData,
  queryAllGuestSystemData
} from '@/api/portal/manager/analysis';
import { getToken } from '@/utils/auth';
export default {
  name: 'AppImageListTempV1',
  props: {
    temp: {
      type: Object,
      default() {
        return {};
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      column: [],
      dataList: [],
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      titleFont: {}
    };
  },
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return {
          position: 'absolute',
          left: '0px',
          right: '0px',
          top: '0px',
          bottom: '0px'
        };
      }
      return { height: this.temp.defaultHeight };
    },
    headerStyle() {
      if (this.temp.sfzdy === true) {
        return {
          backgroundColor: this.temp.backgroundColor,
          color: this.temp.fontColor
        };
      }
      return {};
    },
    liStyle() {
      const width = (100 / this.column.row).toFixed(2);
      const height = (
        100 / Math.ceil(this.column.size / this.column.row)
      ).toFixed(2);
      return { width: width + '%', height: height + '%' };
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        };
        this.titleFont = { color: newData.lmys };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx);
          this.column = pzx;
          this.initData(newData.data.id);
        } else {
          const pzx = JSON.parse(newData.pzx);
          this.column = pzx;
          this.initData(newData.id);
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initData(data) {
      if (getToken()) {
        queryAllSystemData({ cid: data })
          .then(res => {
            const { data } = res;
            if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemData({ cid: data })
          .then(res => {
            const { data } = res;
            if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      }
    },
    openEntity(data) {
      // 打开详情页
      if (this.column.isOpenEntity) {
        // 允许打开详情页
        const params = [];
        let url = '';
        if (
          undefined !== this.column.entityParams &&
          this.column.entityParams !== null &&
          this.column.entityParams.length > 0
        ) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] };
            params.push(obj);
          });
        }
        if (this.column.entityOpenType === 1) {
          // 地址参数的方式打开
          url = data[this.column.entityUrl];
        }
        if (this.column.entityOpenType === 2) {
          // 地址传参查询
          if (
            undefined !== this.column.params &&
            this.column.params !== null &&
            this.column.params.length > 0
          ) {
            url = this.column.entityUrl;
            this.column.params.forEach(item => {
              url = url.replace(
                new RegExp(
                  '\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'
                ),
                data[item.column]
              );
            });
          }
        }
        const object = {
          title: data[this.column['title']],
          params: params,
          setting: this.temp,
          entityTemp: this.column.entityTemplateId,
          temp: this.column.entityTemp,
          openType: this.column.entityOpenType,
          url: url
        };
        this.$emit('fetch-entity', object);
      }
    },
    openMore() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank');
        }
      } else if (this.temp.dkfs === 1) {
        this.$emit('fetch-data', this.temp);
      } else {
        console.log('');
      }
    }
  }
};
</script>

<style scoped>
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-flow: column nowrap;
  background-color: #ffffff;
  box-sizing: border-box;
  overflow: hidden;
}
.custom-header {
  width: 100%;
  height: 40px;
  flex: 0 1 auto;
  padding: 0px 10px;
  box-sizing: border-box;
  line-height: 40px;
  cursor: pointer;
  border-bottom: 1px solid #ebeef5;
}
.header-body {
  width: 100%;
  height: 41px;
  display: flex;
  flex-flow: row nowrap;
}
.header-icon {
  width: 20px;
  height: 40px;
  flex: 0 1 auto;
  text-align: center;
  color: #409eff;
}
.header-icon.more {
  width: 40px;
}
.header-title {
  height: 40px;
  flex: 1 1 auto;
}
.header-line {
  position: absolute;
  left: 0px;
  top: 0px;
  height: 40px;
  width: 4px;
  background-color: #0a84ff;
}
.title-span {
  height: 40px;
  padding: 12px 5px;
  box-sizing: border-box;
  line-height: 40px;
  text-align: center;
  position: relative;
  font-weight: bolder;
}
.custom-right {
  flex: 1 1 auto;
  width: 100%;
  border-left: 1px solid #e2e2e2;
}
.custom-right-li {
  width: 33.3333%;
  border-bottom: 1px solid #e2e2e2;
  border-right: 1px solid #e2e2e2;
  padding: 5px 10px;
  box-sizing: border-box;
  cursor: pointer;
  height: 50%;
  float: left;
  position: relative;
}
.custom-li-image {
  width: 100%;
  height: 40px;
  text-align: center;
}
.custom-li-title {
  width: 100%;
  text-align: center;
  font-size: 13px;
  line-height: 15px;
  height: 30px;
  overflow: hidden;
}
.custom-li-body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.custom-image {
  width: 30px;
}
</style>
